package com.button;

import com.app.entity.ExcelRowData;
import com.app.entity.TemplateEntity;
import com.app.util.DatabaseUtil;
import com.app.util.ExcelUtils;
import com.app.util.FreemarkerUtil;
import com.app.util.MyStringUtils;
import com.button.entity.ButtonEntity;
import com.button.entity.ButtonTemplateEntity;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class ButtonGenerator {

    public static void main(String[] args) throws IOException, InvalidFormatException {
        List<ButtonEntity> buttonEntities = new ArrayList<>();
        List<String> strings = convertExcelToExcelRowData("src/main/resources/button模板.xlsx");
        for (int i = 0; i < strings.size(); i++) {
            String html = strings.get(i);
            int methodStartIndex = html.indexOf("(click)=") + 9;
            int methodEndIndex = html.indexOf("\"", methodStartIndex);
            String methodName = html.substring(methodStartIndex, methodEndIndex);
            int textStartIndex = html.indexOf("text=") + 6;
            int textEndIndex = html.indexOf("\"", textStartIndex);
            String textName = html.substring(textStartIndex, textEndIndex);
            if (textName.length() == 2) {
                textName = textName.charAt(0) + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + textName.charAt(1);
            }
            ButtonEntity buttonEntity = new ButtonEntity();
            buttonEntity.setClcikMenthodNames(methodName);
            buttonEntity.setButtonText(textName);
            buttonEntities.add(buttonEntity);
            System.out.println(methodName);
            System.out.println(textName);
        }
        ButtonTemplateEntity buttonTemplateEntity = new ButtonTemplateEntity();
        buttonTemplateEntity.setButtonEntitys(buttonEntities);
        generateButtonHtml(buttonTemplateEntity);
    }



    public static List<String> convertExcelToExcelRowData(String filepath)
            throws EncryptedDocumentException, InvalidFormatException, IOException {
        Workbook workbook = ExcelUtils.getWorkbook(filepath);
        List<String> strs = new ArrayList<>();
        if (workbook != null) {
            Sheet sheet = workbook.getSheetAt(0);
            if (sheet != null) {
                int rowNos = sheet.getLastRowNum();// 得到excel的总记录条数
                for (int i = 0; i <= rowNos; i++) {// 遍历行
                    Row row = sheet.getRow(i);
                    if (row != null) {
                        int columNos = row.getLastCellNum();// 表头总共的列数
                        for (int j = 0; j < columNos; j++) {
                            if (j > 1) {
                                break;
                            }
                            Cell cell = row.getCell(j);
                            if (cell != null) {
                                cell.setCellType(CellType.STRING);
                                String stringCellValue = cell.getStringCellValue();
                                strs.add(stringCellValue);
                                System.out.println(stringCellValue);
                            }
                        }
                    }

                }

            }
        }
        return strs;
    }

    public static void generateButtonHtml(ButtonTemplateEntity templateEntity) {
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_26);
        try {
            //  指定模板所在的classpath目录
            cfg.setClassForTemplateLoading(ButtonGenerator.class, "/template");
            //  指定模板
            Template t = cfg.getTemplate("buttonTemplate.ftl");
            String projectDirectory = "mybatis-generator-core/generatorCode/resources/";
            File f = new File(System.getProperty("user.dir") + "/" + projectDirectory);
            if (!f.exists()) {
                f.mkdirs();
            }
            String filePath = System.getProperty("user.dir") + "/" + projectDirectory + "button.html";
            File file = new File(filePath);
            FreemarkerUtil.generateFreemarkerFile(file, t, templateEntity);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
